package com.sanyuan.cloud.module.system.controller.app.ip;

import cn.hutool.core.lang.Assert;
import com.sanyuan.cloud.framework.apilog.core.annotation.ApiAccessLog;
import com.sanyuan.cloud.framework.common.pojo.CommonResult;
import com.sanyuan.cloud.framework.common.util.object.BeanUtils;
import com.sanyuan.cloud.framework.ip.core.Area;
import com.sanyuan.cloud.framework.ip.core.utils.AreaUtils;
import com.sanyuan.cloud.module.system.controller.app.ip.vo.AppAreaNodeRespVO;
import com.sanyuan.cloud.module.system.controller.app.ip.vo.AreaLocationReqVO;
import com.sanyuan.cloud.module.system.controller.app.ip.vo.AreaLocationRespVO;
import com.sanyuan.cloud.module.system.controller.app.ip.vo.DistrictRespVO;
import com.sanyuan.cloud.module.system.service.ip.AppAreaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.util.List;

import static com.sanyuan.cloud.framework.common.pojo.CommonResult.success;

@Tag(name = "小程序 - 地区")
@RestController
@RequestMapping("/system/area")
@Validated
public class AppAreaController {

    @Resource
    private AppAreaService appAreaService;

    @GetMapping("/tree")
    @Operation(summary = "获得地区树")
    @PermitAll
    public CommonResult<List<AppAreaNodeRespVO>> getAreaTree() {
        Area area = AreaUtils.getArea(Area.ID_CHINA);
        Assert.notNull(area, "获取不到中国");
        return success(BeanUtils.toBean(area.getChildren(), AppAreaNodeRespVO.class));
    }

    @GetMapping("/getDistrict")
    @Operation(summary = "行政区域查询")
    public CommonResult<List<DistrictRespVO>> getDistrict(@RequestParam(value = "citycode", required = false) String citycode) {
        return success(appAreaService.getDistrict(citycode));
    }

    @ApiAccessLog
    @PostMapping("/getAreaLocation")
    @Operation(summary = "获取地理位置信息")
    public CommonResult<AreaLocationRespVO> getAreaLocation(@RequestBody @Validated AreaLocationReqVO reqVO) {
        return success(appAreaService.getAreaLocation(reqVO));
    }
}
